package com.joysuch.wwyt.api.mh.service.impl;

import com.joysuch.wwyt.api.mh.entity.MonitPointConfig;
import com.joysuch.wwyt.api.mh.repository.MonitPointConfigRepository;
import com.joysuch.wwyt.api.mh.service.MonitPointConfigService;
import com.joysuch.wwyt.config.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class MonitPointConfigServiceImpl  implements MonitPointConfigService  {

    @Resource
    private RedisClient redisClient;

    @Autowired
    private MonitPointConfigRepository monitPointConfigRepository;

    private static final String WWYT_API_MH_MONIT_POINT="wwytapi:mh:monit:point";


    @Override
    public RMap<String, String> getMointPointConfigMap(String sendMethod){
        String key = WWYT_API_MH_MONIT_POINT+":"+sendMethod;
        RMap<String, String> map = redisClient.getMap(key);
        log.info("... get mh monit config data from cache map =" + ((map == null)? "" : map.size()));
        if(map == null || map.size() ==0){
            List<MonitPointConfig> list = monitPointConfigRepository.findAllBySendMethodOrderByCompanyCode(sendMethod);
            for(MonitPointConfig m: list){
                map.put(m.getPointCode(), m.getPointNo()+","+ m.getCorrectionFactor());

            }

        }
        return map;
    }


    @Override
    public void clearCache(String sendMethod){
        redisClient.removeKey(WWYT_API_MH_MONIT_POINT+":"+sendMethod);
    }

}
